package com.leyou.sh.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.sh.item.dto.CategoryDTO;
import com.leyou.sh.item.entity.CategoryBrand;
import com.leyou.sh.item.service.CategoryBrandService;
import com.leyou.sh.item.entity.Category;
import com.leyou.sh.item.mapper.CategoryMapper;
import com.leyou.sh.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author balisong_fcs
 * @date 2020/12/27 13:40
 * @desc
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private CategoryBrandService categoryBrandService;
    @Override
    public CategoryDTO queryCategoryById(Long id) {
        return new CategoryDTO(this.getById(id));
    }

    @Override
    public List<CategoryDTO> queryCategoryByIds(List<Long> ids) {
        return CategoryDTO.convertEntityList(this.listByIds(ids));
    }

    @Override
    public List<CategoryDTO> queryCategoryByPid(Long pid) {
        return CategoryDTO.convertEntityList(this.query().eq("parent_id", pid).list());
    }

    @Override
    public List<CategoryDTO> queryCategoryByBrandId(Long id) {
        List<CategoryBrand> categoryBrandList = categoryBrandService.query().eq("brand_id", id).list();
        if (CollectionUtils.isEmpty(categoryBrandList)) {
            return Collections.emptyList();
        }
        /**
         *@desc 隐式传参,获取id的list集合
         */
        List<Long> categoryIds = categoryBrandList.stream().map(CategoryBrand::getCategoryId).collect(Collectors.toList());
        return CategoryDTO.convertEntityList(this.listByIds(categoryIds));
    }

    @Override
    public Map<String,List<String>> loadCategory() {
        List<Category> parent = this.query().eq("sort", 1).list();
        Map<String, List<String>> map1 = new HashMap<>();
        for (Category category : parent) {
                List<Category> child = this.query().eq("parent_id", category.getId()).list();
                List<String> nameList = child.stream().map(Category::getName).collect(Collectors.toList());
            map1.put(category.getName(), nameList);
        }
        return map1;
    }

   /* @Override
    public List<CategoryDTO> queryCategoryByBId(Long id) {
        return CategoryDTO.convertEntityList(this.baseMapper.queryCategoryByBrandId(id));
    }*/
}
